package threadBaseUse;

import java.util.concurrent.*;

/**
 * @Author: WanqingLiu
 * @Date: 2023/02/04/11:49
 * 通过实现 Callable 接口创建线程
 * Callable _ 可以定义返回值，可以提交异常
 */

public class TestCallable implements Callable<Boolean> {

    // 重写 call 方法
    @Override
    public Boolean call() throws Exception {
        System.out.println("线程" + Thread.currentThread().getName());
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();

        // 1. 创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);

        // 2. 提交执行
        Future<Boolean> r1 = service.submit(t1);
        Future<Boolean> r2 = service.submit(t2);
        Future<Boolean> r3 = service.submit(t3);

        // 3. 获取结果
        System.out.println("r1" + (boolean)r1.get());
        System.out.println("r2" + (boolean)r2.get());
        System.out.println("r3" + (boolean)r3.get());

        // 4. 关闭服务
        service.shutdown();
    }
}


